import type { ConfigEnv, UserConfigExport } from "vite";
import { fileURLToPath, URL } from "node:url";
import Tailwindcss from "@tailwindcss/vite";
import react from "@vitejs/plugin-react";
import { defineConfig, loadEnv } from "vite";
import ViteCompression from "vite-plugin-compression";
import { createHtmlPlugin } from "vite-plugin-html";
import { vitePluginFakeServer } from "vite-plugin-fake-server";
import { VitePWA } from "vite-plugin-pwa";

// https://vite.dev/config/
export default ({ mode }: ConfigEnv): UserConfigExport => {
  // eslint-disable-next-line node/prefer-global/process
  const env: Record<string, string> = loadEnv(mode, process.cwd());

  return defineConfig({
    server: {
      host: "0.0.0.0",
      cors: true,
      port: 8088,
      proxy: {
        "/api": {
          target: env.VITE_APP_BASE_URL,
          changeOrigin: true,
          ws: false,
          rewrite: (path: string) => path.replace(/^\/api/, "")
        }
      }
    },
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url))
      }
    },
    plugins: [
      react(),
      Tailwindcss(),
      ViteCompression({
        threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
        algorithm: "gzip", // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
        ext: ".gz", // 生成的压缩包后缀
        deleteOriginFile: false // 压缩后是否删除源文件
      }),
      vitePluginFakeServer({
        enableDev: true,
        enableProd: true,
        logger: false
      }),
      createHtmlPlugin({
        minify: true,
        inject: {
          data: { title: env.VITE_APP_TITLE }
        }
      }),
      VitePWA({
        registerType: "autoUpdate",
        devOptions: { enabled: true },
        manifest: {
          name: env.VITE_APP_TITLE,
          short_name: env.VITE_APP_TITLE,
          theme_color: "oklch(0.145 0 0)",
          // PWA 的 icon 像素尺寸为 600*600 即可适配以下配置尺寸，建议使用 .png 格式
          icons: [
            {
              src: "/react.png",
              sizes: "192x192",
              type: "image/png"
            },
            {
              src: "/react.png",
              sizes: "512x512",
              type: "image/png"
            },
            {
              src: "/react.png",
              sizes: "512x512",
              type: "image/png",
              purpose: "any maskable"
            }
          ]
        }
      })
    ],
    build: {
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
          properties: true,
          keep_fnames: true
        }
      },
      sourcemap: false,
      // 禁用 gzip 压缩大小报告，可略微减少打包时间
      reportCompressedSize: false,
      // 规定触发警告的 chunk 大小
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        output: {
          chunkFileNames: "assets/js/[name]-[hash].js",
          entryFileNames: "assets/js/[name]-[hash].js",
          assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
        }
      }
    }
  });
};
